<!-- 收文管理——>待处理收文——>收文信息 -->
<template>
  <view>
    <van-collapse v-model="tableShow" accordion :border="border">
      <van-collapse-item icon="label-o" title="基本信息" title-class="text-bold" name="jbxx">
        <van-cell-group :border="border" v-if="detailObj != null">
          <van-cell title="文件标题" :value="detailObj.FW_TITLE" value-class="text-sm"/>
          <van-cell title="秘密密级" :value="detailObj.CLASSIFICATIONNAME" value-class="orange text-sm"/>
          <van-cell title="紧急程度" :value="detailObj.URGENCYNAME" value-class="orange text-sm"/>
          <van-cell title="来文号" :value="detailObj.FILE_SIZE" value-class="text-sm"/>
          <van-cell title="来文单位" :value="detailObj.FWDEPTNAME" value-class="text-sm"/>
          <van-cell title="收文单位" :value="swDeptName" value-class="text-sm"/>
          <van-cell title="发文备注" :value="detailObj.FW_REMARK" value-class="text-sm"/>
        </van-cell-group>
        <view class="opinion" v-if="detailBody !=null">
          <text class="opinion-left">正文</text>
          <view class="opinion-right">
            <view v-for="(item,index) in detailBody.doc" :key="item.attachName" @click="lx.openFile(item,proInstId)">
              <text class="text-blue">{{ item.attachName }}</text>
            </view>
          </view>
        </view>
        <view class="opinion" v-if="detailAttachment != undefined && detailAttachment.length > 0 ">
          <text class="opinion-left">附件</text>
          <view class="opinion-right" >
            <view v-for="(item,index) in detailAttachment" @click="lx.openFile(item, item.attachId)">
              <text class="text-blue">{{ item.attachName }}</text>
            </view>
          </view>
        </view>
        <view class="opinion" v-if="relation != undefined && relation.length>0 ">
          <text class="opinion-left">附加公文</text>
          <view class="opinion-right">
            <view v-for="(item,index) in relation" @click="lx.openFile(item, item.attachId)">
              <text class="text-blue">{{ item.title }}</text>
            </view>
          </view>
        </view>
      </van-collapse-item>
      <van-collapse-item icon="label-o" title="处理收文" title-class="text-bold" name="clsw">
        <van-cell-group :border="border">
          <van-cell is-link required title="签收处理" :value="typeName" @click="showType = true"/>
          <van-action-sheet v-model="showType" :actions="typeList" @select="onSelectType" cancel-text="取消"/>

          <van-cell is-link required title="收文号" v-show="showSwbh" v-model="swcode" @click="showGenerate('sw')"/>
          <generate @closeGenerate="closeGenerate" @saveGenerate="saveGenerate" :tagType="tagType"
                    :showGenerate="showGen" :generateParamObject="paramObject"></generate>

          <van-cell required title="经办人" :value="userName" aria-readonly="true"/>

          <van-field required label="联系电话" v-model="formData.PHONE" type="tel" input-align="right"/>

          <van-field label="收文备注" v-model="formData.SW_REMARKS" rows="2" autosize type="textarea" input-align="right">
            <template #label>
              <span v-if="showSwbh">收文备注</span>
              <span v-else>拒收原因</span>
            </template>
          </van-field>
        </van-cell-group>
        <view class="padding-top-lg padding-bottom-lg">
          <van-row type="flex" justify="space-around">
            <van-col span="10">
              <van-button type="info" round plain hairline block @click="toClose">取消</van-button>
            </van-col>
            <van-col span="10">
              <van-button type="info" round block @click="toSave">提交</van-button>
            </van-col>
          </van-row>
        </view>
      </van-collapse-item>
    </van-collapse>
  </view>
</template>

<script>
import generate from 'pages/jgj/components/receiveDoc/compo-generate';

export default {
  components: {
    generate,
  },

  onLoad(option) {
    this.bizId = option.bizId
    this.uuId = option.uuId
    if (this.bizId != undefined && this.bizId != '') {
      // 通过bizId获取详情
      this.getDetail(this.bizId)
    }
  },

  data() {
    return {
      border: false,
      tableShow: "jbxx",
      bizId: '',     // 公文biz_id
      uuId: '',      // 待办列表中的UUID
      detailObj: {}, // 公文详情
      detailBody: {},       // 正文
      detailAttachment: [], // 附件
      relation: [],         // 附加公文
      formData: {},         // 提交的数据
      swDeptName: '',       // 收文单位
      userName: '',					// 当前登录人姓名
      showType: false,      // 显示签收类型
      typeName: '',         // 签收类型
      typeList: [           // 签收类型
        {"id": "1", "name": "接收"},
        {"id": "0", "name": "拒收"}
      ],
      showSwbh: true,       // 默认显示收文编号
      showGen: false,       // generate显示
      tagType: '',          // sw生成收文字号，lw生成来文字号
      paramObject: {},      // 用于回显
      swcode: '',           // 收文编号

    };
  },

  created() {
    this.swDeptName = this.cache.get('userInfo').deptName
    this.userName = this.cache.get('userInfo').userName
    // 公文id及当前登录人id
    this.formData.UUID = this.uuId
    this.formData.AGENT = this.cache.get('userInfo').userId
  },

  methods: {
    // 获取详情
    getDetail(bizId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/doc/fwend/getListData`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'ID': bizId,
          'page': 1,
          'rows': 1
        })
      }).then(({data}) => {
        if (data.code == 200) {
          if (data.data != null && JSON.stringify(data.data) != '{}'
              && data.data.rows != null && data.data.rows.length > 0) {
            this.detailObj = data.data.rows[0]
            // 获取正文及附件
            let relateId = null
            if (this.detailObj.FW_PRO_INST_ID != null && this.detailObj.FW_PRO_INST_ID != '') {
              relateId = this.detailObj.FW_PRO_INST_ID
            } else {
              relateId = this.detailObj.ID
            }
            if (relateId != null) {
              this.getCaacBody(relateId)
              this.getCaacAttachment(relateId)
              this.getRelation(relateId)
            }
          }
        }
      })
    },

    // 获取正文数据
    getCaacBody(relateId) {
      var _this = this
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/maintext/proInstId/` + relateId),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag
        })
      }).then(({data}) => {
        if (data.code == 200) {
          _this.detailBody = data.data
        }
      })
    },

    // 获取附件数据
    getCaacAttachment(relateId) {
      var _this = this
      // attachmentinfoOrderByNew
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/attachmentupload/attachmentinfoOrderBy`),
        method: 'get',
        params: this.$http.adornParams({
          'bizId': relateId,
          'attachTypeId': 'pageBody_uploadFile',
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag
        })
      }).then(({data}) => {
        if (data.code == 200) {
          _this.detailAttachment = data.data
        }
      })
    },

    // 获取附加公文
    getRelation(relateId) {
      var _this = this
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjComponent/relationfile/relation`),
        method: 'get',
        params: this.$http.adornParams({
          'docId': relateId,
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag
        })
      }).then(({data}) => {
        if (data.code == 200) {
          if (data.data != null) {
            _this.relation = data.data
          }
        }
      })
    },

    // 签收事件
    onSelectType(item) {
      this.formData.ACCEPT_TYPE = item.id
      this.typeName = item.name
      this.showType = false

      if (item.id == '0') {
        this.showSwbh = false
      } else {
        this.showSwbh = true
      }
    },

    // 显示字号组件
    showGenerate(type) {
      this.tagType = 'sw'
      this.showGen = true
      this.paramObject.swId = this.formData.UUID
      this.paramObject.swWordId = this.formData.SW_WORD_ID
      this.paramObject.swWord = this.formData.SW_WORD
      this.paramObject.swNum = this.formData.SW_NUM
      this.paramObject.swYear = this.formData.SW_YEAR
    },
    // 关闭字号组件
    closeGenerate() {
      this.showGen = false
    },

    // 保存字号
    saveGenerate(data) {
      this.formData.SW_WORD_ID = data.swWordId
      this.formData.SW_WORD = data.swWord
      this.formData.SW_YEAR = data.swYear
      this.formData.SW_NUM = data.swNum

      this.swcode = this.generateCode(data.swWord, data.swYear, data.swNum)
      this.showGen = false
    },
    // 生成编号
    generateCode(word, year, num) {
      if (word != null && word != ''
          && year != null && year != ''
          && num != null && num != '') {
        return word + '〔' + year + '〕' + num + '号'
      }
    },

    toClose() {
      uni.navigateTo({
        url: "/pages/jgj/receive/todoDoc"
      })
    },

    toSave() {
      if (this.validateForm()) {
        this.$httploading({
          url: this.$httploading.adornUrl('/caac/oa/doc/fwend/fwSendLogSave'),
          method: 'post',
          params: this.$httploading.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,   // 固定值
            'systemFlag': this.cache.get('userInfo').systemFlag, // 固定值
            'data': JSON.stringify(this.formData)
          }),
        }).then(({data}) => {
          if (data.code == 200) {
            if (this.formData.ACCEPT_TYPE == '1') {
              // 接收后跳转到收文办理页面
              uni.navigateTo({
                url: "/pages/jgj/receive/index?proInstId=" + data.data.proInstId + "&from=swbl"
              })
            } else {
              // 拒收后跳转待处理收文页面
              this.toClose()
            }
          } else {
            this.$toast(data.message + ' code:' + data.code)
          }
        })
      }
    },
    // 表单校验
    validateForm() {
      if (this.typeName == null || this.typeName == '') {
        this.$toast('请选择签收处理')
        return false
      }
      if (this.formData.ACCEPT_TYPE == '1') {
        if (this.swcode == null || this.swcode == '') {
          this.$toast('请选择收文号')
          return false
        }
      }
      if (this.formData.PHONE == null || this.formData.PHONE == '') {
        this.$toast('请填写联系电话')
        return false
      }
      return true
    },
  },
};
</script>

<style scoped>
	
	/* 意见相关样式 */
	.opinion{
		display: flex;
		flex-direction: column;
		margin: 0 32rpx;
		padding: 20rpx 0rpx;
		overflow: hidden;
		font-size: 28rpx;
		line-height: 24px;
		border-bottom: 1px solid #ebedf0;
		
	}
	.opinion .opinion-title{
		text-align: left;
		padding-bottom: 14rpx;
	}
	.opinion .opinion-box{
		margin-bottom: 20rpx;
	}
	.opinion .opinion-left{
		text-align: left;
		color: #323233;
	}
	.opinion .opinion-right{
		text-align: right;
		color: #969799;
	}
	.opinion .opinion-right .opinion-user{
		font-weight: 400;
		color: #000000;
	}
	.opinion .opinion-right .opinion-date{
		color: #323233;
		margin-left: 20rpx;
	}
	
	.van-cell__title {
		min-width: 10%;
	}
	
	/deep/ .van-cell__title.van-field__label{
	    min-width: 20%;
	}
	
	
	/deep/ .van-collapse-item__content {
		padding: 0px;
		color: #969799;
		font-size: 14px;
		line-height: 1.5;
		background-color: #fff
	}
</style>
